// const Realm = require('realm');
// 新建表模型
export const BookSchema = {
    name: 'BookDir',
    version: 0,
    primaryKey: 'uid',  //使用时间戳  // 官方没给出自增长的办法,而且一般不会用到主键,这也解决了重复访问的问题,而且实际开发中我们不需要主键的,让服务端管就是了
    properties: {
        // Realm supports the following basic types: bool, int, float, double, string, data, and date.
        uid: 'string',//bookId // new Date().getTime().toString()
        title: 'string',
        path: {type: 'string', default: ''},
        type: {type: 'string', default: 'txt'},
        size: {type: 'string', default: ''},
        progress: {type: 'string', default: '0%'},
        author: {type: 'string', default: ''},
        lastTime: {type: 'string', default: ''},
        image: {type: 'string', default: ''},
    }
};
export const PTest = {
    name: 'p1',
    version: 10,
    primaryKey: 'uid',  //使用时间戳  // 官方没给出自增长的办法,而且一般不会用到主键,这也解决了重复访问的问题,而且实际开发中我们不需要主键的,让服务端管就是了
    properties: {
        // Realm supports the following basic types: bool, int, float, double, string, data, and date.
        uid: 'string',//bookId // new Date().getTime().toString()
        name: 'string',
        author: {type: 'string', default: 'author'},
        progress: {type: 'string', default: '0%'},
        lastTime: {type: 'string', default: ''},
        image: {type: 'string', default: ''},
        size: {type: 'string', default: ''},
        type: {type: 'string', default: 'txt'},
        // tel_number: {type: 'string', default: '156xxxxxxxx'},   // 添加默认值的写法
    }
};


class DBUtils {
    realm;

    constructor(schema) {
        this.realm = new Realm({schema: [schema], schemaVersion: schema.version});
        // this._migration(schema)
    }

    _migration = (schema) => {
        Realm.open({
            schema: [schema], schemaVersion: 5, migration: (oldRealm, newRealm) => {
                // 只有当schemaVersion小于1的时候才会执行if中代码
                alert(oldRealm.schemaVersion + '');
                if (oldRealm.schemaVersion < 1) {
                    console.log('_migration');
                    const oldObjects = oldRealm.objects(schema.name);
                    const newObjects = newRealm.objects(schema.name);
                    // 遍历所有对象，并将新的模型中设置属性
                    alert('_migration');
                    for (let i = 0; i < oldObjects.length; i++) {
                        newObjects[i].author = oldObjects[i].name
                    }
                } else if (oldRealm.schemaVersion < 2) {
                    const oldObjects = oldRealm.objects(schema.name);
                    const newObjects = newRealm.objects(schema.name);
                    alert('_migration');
                    for (let i = 0; i < oldObjects.length; i++) {
                        newObjects[i].progress = '8%';
                    }
                }
            }
        }).then(realm => {
            this.realm = realm;
            const newBook = realm.objects(schema.name)[0].author;
            // alert(newBook);
        });

    };

    _write = (schema, data) => {
        try {
            data.uid = new Date().getTime().toString();
            data.progress = '0%';
            this.realm.write(async () => {
                this.realm.create(schema.name, data);
            });
            return data;
        } catch (e) {
            alert(e);
        }
    };

    _query(schema) {
        // 查询所有数据
        try {
            let datas = this.realm.objects(schema.name);
            return datas;//返回的是对象,但可以数组方法获取
        } catch (e) {
            alert(e);
        }
    };

    _queryLastUid(schema) {
        // 查询所有数据
        try {
            let datas = this.realm.objects(schema.name);
            // alert(JSON.stringify(datas[datas.length - 1].uid));
            // alert(JSON.stringify(datas[i]));
            return datas;
        } catch (e) {
            alert(e);
        }
    }

    // 根据条件查询
    _queryFilter = (schema, filter) => {
        try {
            let datas = this.realm.objects(schema.name);
            let data = datas.filtered(filter);
            // let tanDogs = dogs.filtered('color = "tan" AND name BEGINSWITH "B"');
            return data;
        } catch (e) {
            alert(e);
        }
    };

    _upData = async (schema, data) => {
        try {
            this.realm.write(() => {
                // 方式一
                this.realm.create(schema.name, data, true);//有主键

                // // 方式二:如果表中没有主键,那么可以通过直接赋值更新对象
                // // 获取Person对象
                // let Persons = realm.objects('Person2');
                // // 设置筛选条件
                // let Person2 = Persons.filtered('name == 苍井空');
                // // 更新数据
                // Person2.name = '黄鳝门'
            })
        } catch (e) {
            alert(e);
        }
    };

    // 删除
    _delForUid = (schema, uid) => {
        try {
            if (uid == null) return;
            this.realm.write(() => {
                let datas = this.realm.objects(schema.name);
                let result = datas.filtered('uid = "' + uid + '"');
                // let tanDogs = dogs.filtered('color = "tan" AND name BEGINSWITH "B"');
                // alert(JSON.stringify(result));
                this.realm.delete(result);
            })
        } catch (e) {
            alert(e);
        }
    };
    // 删除全部
    _clearAll = async (schema) => {
        try {
            this.realm.write(() => {
                // 获取Person对象
                let datas = this.realm.objects(schema.name);
                // 删除
                this.realm.delete(datas);
            })
        } catch (e) {
            alert(e);
        }
    };
}

export default DBUtils;